myApp.controller('asset_add', function ($rootScope, $scope, $http, $uibModalInstance, httpService, formBoxData) {
	
	/*关闭的图片*/
	$scope.closeImg={'img':'images/close-default.png','defaultImg':'images/close-default.png','downImg':'images/close-btndown.png','hoverImg':'images/close-hover.png'};
    $scope.title = formBoxData.title;
    $scope.formData = {
    };
	$scope.data = [{
		'id': 1,
		'title': 'node1',
		'nodes': [
			{
				'id': 11,
				'title': 'node1.1',
				'nodes': [
					{
						'id': 111,
						'title': 'node111',
						'nodes': []
					},
					{
						'id': 222,
						'title': 'node222',
						'nodes': []
					},
					{
						'id': 333,
						'title': 'node333',
						'nodes': []
					}
				]
			},
			{
				'id': 12,
				'title': 'node1.2',
				'nodes': []
			}
		]
	}, {
		'id': 2,
		'title': 'node2',
		'nodrop': true, // An arbitrary property to check in custom template for nodrop-enabled
		'nodes': [
			{
				'id': 21,
				'title': 'node2.1',
				'nodes': []
			},
			{
				'id': 22,
				'title': 'node2.2',
				'nodes': []
			}
		]
	}, {
		'id': 3,
		'title': 'node3',
		'nodes': [
			{
				'id': 31,
				'title': 'node3.1',
				'nodes': []
			}
		]
	}];
    $scope.inputObject = {};
	$scope.treeSelectIds = [];
	$scope.treeSelectTitle = [];
	var selIdx,selTitle ;
	//点击获取列表
	$scope.checkTree=function(t){
		if(t.nodes.length>0){
		}else{
			selIdx = $scope.treeSelectIds.indexOf(t.id);
			selTitle = $scope.treeSelectTitle.indexOf(t.id);
			if(selIdx>=0){
				$scope.treeSelectIds.splice(selIdx,1);
				$scope.treeSelectTitle.splice(selTitle,1);
			}else{
				$scope.treeSelectIds.push(t.id);
				$scope.treeSelectTitle.splice(t.title);
			}
		}
	}
/*	$scope.treeCheckAll=function(t){
		event.stopPropagation();
		angular.forEach(t.nodes,function (item,idx) {
			selIdx = $scope.treeSelectIds.indexOf(item.id);
			if(selIdx>=0){
				$scope.treeSelectIds.splice(selIdx,1)
			}else{
				$scope.treeSelectIds.push(t.id)
			}
		})
	}*/
	// 获取人员列表
    $scope.getPeopleList = function () {
        $http({
            method: 'GET',
            url: 'json/peopleList.json'
        }).success(function (response) {
            $scope.peopleList = response.data;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };
    $scope.setFormUser = function () {
        $scope.formData.userId = $scope.inputObject.user.id;
    };

    // 获取管理员列表
    $scope.getManagerList = function () {
        $http({
            method: 'GET',
            url: 'json/managerList.json'
        }).success(function (response) {
            $scope.managerList = response.data;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };
    $scope.setFormManager = function () {
        $scope.formData.manager = $scope.inputObject.manager.id;
    };

    // 获取资产类型列表
    $scope.getAssetTypeList = function () {
        $http({
            method: 'GET',
            url: 'json/assetTypeList.json'
        }).success(function (response) {
            $scope.assetTypeList = response.data;
            $scope.formData.assetType = undefined;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };

    function formInit() {
        $scope.getPeopleList();
        $scope.getManagerList();
        $scope.getAssetTypeList();
    }
    formInit();

    // 日期选择
    $scope.datePopup = {
        opened: false
    };
    $scope.datePopup2 = {
        opened: false
    };
    $scope.datePopup3 = {
        opened: false
    };

    // 选择图片
    $scope.selectImg = function(obj) {
    	console.log($scope.formData.userId);
        $file = $(obj);
        var file = obj.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);// 读出 base64
        reader.onload = (function(evt) {
            $scope.assetPicBase64 = evt.target.result;
        });
    };

    // 保存
    $scope.ok = function () {
        !$scope.formData.assetCost  ? $scope.form.assetCost.$isTure=true : $scope.form.assetCost.$isTure=false ;
		!$scope.formData.buyType  ? $scope.buyType=true : $scope.buyType=false;
        !$scope.formData.buyTime  ? $scope.buyTime=true : $scope.buyTime=false;
		!$scope.formData.modelNumber  ? $scope.form.modelNumber.$isTure=true : $scope.form.modelNumber.$isTure=false ;
		!$scope.formData.brand   ? $scope.form.brand.$isTure=true : $scope.form.brand.$isTure=false ;
		!$scope.formData.useCompany  ? $scope.useCompany=true : $scope.useCompany=false;
		if(
            $scope.formData.assetCost &&
			$scope.formData.buyType &&
			$scope.formData.buyTime &&
			$scope.formData.modelNumber &&
			$scope.formData.brand &&
			$scope.formData.useCompany
        ){
			httpService.postRequest('assets/insertAssetsManagement', $scope.formData).then(function(res){
				var result = res.data;
				if(res.resultCode === 0){
					var assetsOperation = {
						assetsId: result.id,
						dealStatus: '入库'
					};
					formBoxData.confirmFunc();
					httpService.insertAssetsOperation(assetsOperation);
				}
				console.log(res);
				$scope.cancel();
			});
		}
    };
    // 取消
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});
